#!/bin/bash

reg_file="regression_file.l"
log_file="reg_failed.log"
original_rtl_path="../../../design/rtl"
original_commip_path="../../../design/common_ip"
total_tasks=0
remain_tasks=0
completed_tasks=0
progress=0

test_name=(
	"mac_single_port_polling_smoke" 
	"mac_multi_port_transfer_smoke"
	"mac_crc_error_smoke"
	"mac_lut_study_smoke"
	"switch_random_test"
	"cover_case"
	"perf_test")

test_total_times=("0" "0" "0" "0" "0" "0" "0")
read_point=0

function help {
	printf "\e[33mUsage:\r\n"
	printf "	RUN: ./run.sh -t TESTNAME (-s SEED)\r\n"
	printf "	VERDI: ./run.sh -v TESTNAME (-s SEED)\r\n"
	printf "	RUN WITH COV: ./run.sh -c TESTNAME\r\n"
	printf "	REGRESS: ./run.sh -r -repeat TIMES -max_job NUMS (-c)\r\n"
	printf "	SHOW COV RESULT: ./run.sh -s\r\n"
	printf "\r\nTESTNAME:\r\n"
	for i in ${test_name[*]}
	do
		printf "	$i\r\n"
	done
	printf "\e[0m"
}


function find_test_name() {
	if [ -z "$1" ]; then
		return 0
	fi

	for i in "${!test_name[@]}"; do
		if [ "${test_name[$i]}" = $1 ]; then
			return 1
		fi
	done
	return 0
}


run_test_case() {
	testcase=$1
	cover_flag=$2
	find_test_name $testcase
	if [ $? -ne 1 ]; then
		return 
	fi
	random=$((1 + RANDOM % 1000000))

	make regression TESTNAME=$testcase FSDB_FLAG=0 COVER=$cover_flag SEED=$random > "logs/$testcase_$random.tmp.log" 2>&1

	completed_tasks=$(($total_tasks - $remain_tasks + 1))
	progress=$(echo "scale=2;
	$completed_tasks * 100 / $total_tasks" | bc)
	echo -en "\033[33m(Progress: $progress%)" 
	if grep -q "TEST PASS" "logs/$testcase_$random.tmp.log"
	then
		echo -e "\033[32mTESTCASE: $testcase SEED: $random TEST PASS!!\033[0m"
	else
		echo -e "\033[31mTESTCASE: $testcase SEED: $random TEST FAILED!!\033[0m" 
		echo -e "\033[31mTESTCASE: $testcase SEED: $random TEST FAILED!!\033[0m" >> "$log_file"
	fi	
	rm "logs/$testcase_$random.tmp.log"
}


if [ "$1" = "-t" ]
then
	find_test_name $2
	if [ $? -eq 1 ]
	then
		if [ "$3" = "-s" ]
		then
			echo "Sim and Run [TEST: $2, SEED: $4]"	
			make TESTNAME=$2 SEED=$4
		else
			echo "Sim and Run [TEST: $2]"
			make TESTNAME=$2
		fi	
	else
		help
	fi
elif [ "$1" = "-c" ]
then
	find_test_name $2
	if [ $? -eq 1 ]
	then
		echo "Sim and Run with Coverage [TEST: $2]"
		make TESTNAME=$2 COVER=1 SEED=$((1 + RANDOM % 1000000))	
	else
		help
	fi
elif [ "$1" = "-v" ]
then
	find_test_name $2
	if [ $? -eq 1 ]
	then
		if [ "$3" = "-s" ]
		then
			echo "Open Verdi [TEST: $2, SEED: $4]"	
			make verdi TESTNAME=$2 SEED=$4
		else
			echo "Open Verdi [TEST: $2]"
			make verdi TESTNAME=$2
		fi	
	else
		help
	fi
elif [ "$1" = "-r" ]
then

	repeat=0

	if [ "$2" = "-repeat" ]
	then
		repeat=$3
	else
		help
		exit
	fi
	if [ "$4" = "-max_job" ]
	then
		testnum=$5
	else
		help
		exit
	fi

	cov=0
	if [ "$6" = "-c" ]
	then
		cov=1
	fi
	while IFS= read -r testcase
	do
		for i in "${!test_name[@]}";
		do
			if [ "${test_name[$i]}" == "$testcase" ]; then
				test_total_times[$i]=$repeat
				let remain_tasks=$remain_tasks+$repeat
				break	
			fi
		done
	done < "$reg_file"
	total_tasks=$remain_tasks

	make mklist_r > /dev/null 2>&1
	if [ ! -f "$reg_file" ]; then
		echo "File not found: $reg_file"
		exit 1
	fi
	: > "$log_file"
	mkdir -p logs
	echo -e "\033[32mRegression start running..."
	echo -e "(Total nums of task is $total_tasks)\n"

	location=0
	while [ $remain_tasks -gt 0 ]
	do
	
		running=$(jobs -r | wc -l)

		if [ $running -eq $testnum ]; then
			continue;
		fi	
		for (( i=$location; i<=6; i++ )); do
			if [ ${test_total_times[$i]} -gt 0 ]; then
				let test_total_times[$i]=test_total_times[$i]-1
				run_test_case ${test_name[$i]} $cov &
				((remain_tasks--))
				((running++))
				if [ $running -eq $testnum ]; then
					if ((i + 1 > 6)); then
						location=0
					else
						location=$((i + 1))
					fi
					break;
				fi
			fi
			location=0
		done
		if [ $remain_tasks -eq 0 ]; then
			break;
		fi
	done
	wait

	rm -r logs
	echo -e "\n\033[32mRegression Finished, failed case list:\033[0m"
	cat "$log_file"
elif [ "$1" = "-s" ]
then
	if [ "$2" = "-d" ]
	then
		make cov COV_TOOL=dve
	elif [ "$2" = "-u" ]
	then
		make cov COV_TOOL=urg
	else
		make cov
	fi
else
	help
fi

